import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import org.apache.bcel.generic.NEW;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.*;

import static java.lang.Integer.parseInt;

public class Run {
    private static Scanner sc = new Scanner(System.in);
    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    /**
     * 设置输出打印到控制台
     */
    static {
        try {
            System.setErr(new PrintStream("err.log"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Console console = System.console();
        if (console != null) {
            sc = new Scanner(console.reader());
        } else {
            System.out.println("Console is unavailable");
        }
    }

    public static void main(String[] args) {
        createTasks();
    }

    static void createTasks() {
        ExecutorService THREAD_POOL = new ThreadPoolExecutor(10, 10, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
        while (true) {
            StandardBuilder builder = new StandardBuilder();
            THREAD_POOL.submit(() -> {
                try {
                    countDownLatch.await();
                    builder.run();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            System.out.print("输入任意字符开始执行任务，Enter键继续搜索资源：");
            String line = sc.nextLine();
            if (!line.isEmpty()) {
                break;
            }
        }
        countDownLatch.countDown();
        System.out.println("开始执行任务...");
    }
}

